package com.qianfeng.plateform2203.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class TimeCostAfterFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 50;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        final RequestContext currentContext = RequestContext.getCurrentContext();
        final long start = (long) currentContext.get("start-time");

        final long end = System.currentTimeMillis();

        final HttpServletRequest request = currentContext.getRequest();
        final String method = request.getParameter("method");

        System.out.println(method+":该接口消耗的时长为:"+(end-start));

        return null;
    }
}
